<template>
    <div class="login-bg">
        <div class="login-container">
            <div class="login-card">
                <div class="login-header">
                    <div class="logo-section">
                        <span class="logo-icon">🐰</span>
                        <h1 class="login-title">爱吃兔</h1>
                        <p class="login-subtitle">用户注册</p>
                    </div>
                </div>

                <el-form :model="form" label-width="0" class="login-form" :rules="formRules" ref="formRef">
                    <div class="form-group">
                        <div class="input-wrapper">
                            <el-input v-model="form.userName" placeholder="请输入用户名" class="custom-input" size="large"
                                :prefix-icon="User" />
                        </div>
                    </div>

                    <div class="form-group">
                        <div class="input-wrapper">
                            <el-input v-model="form.userPass" show-password placeholder="请输入密码" class="custom-input"
                                size="large" :prefix-icon="Lock" />
                        </div>
                    </div>

                    <div class="form-group">
                        <div class="input-wrapper">
                            <el-input v-model="form.userNickName" placeholder="请输入昵称" class="custom-input" size="large"
                                :prefix-icon="UserFilled" />
                        </div>
                    </div>

                    <div class="form-group">
                        <div class="input-wrapper">
                            <el-select v-model="form.roleId" placeholder="请选择角色" class="custom-select" size="large">
                                <el-option label="普通用户" value="1" />
                                <el-option label="管理员" value="2" />
                            </el-select>
                        </div>
                    </div>

                    <div class="form-actions">
                        <el-button type="primary" @click="register" class="register-btn" size="large">
                            <el-icon>
                                <Check />
                            </el-icon>
                            注册
                        </el-button>

                        <div class="login-link">
                            <el-text type="primary" @click="goLogin" class="login-text">
                                <el-icon>
                                    <Back />
                                </el-icon>
                                已有账号？去登录
                            </el-text>
                        </div>
                    </div>
                </el-form>
            </div>
        </div>
    </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue';
import { Read, Write } from '@/components/Http/axiosHepler'
import { ElMessage } from 'element-plus';
import { User, Lock, UserFilled, Check, Back } from '@element-plus/icons-vue';
import { useCounterStore } from "@/stores/counter"
import { useRoute, useRouter } from 'vue-router';

const Router = useRouter();
const Route = useRoute();
const store = useCounterStore();
const router = useRouter();
const formRef = ref();
const form = reactive({
    "userName": "",
    "userPass": "",
    "userNickName": "",
    "roleId": "",
});

const formRules = {
    userName: [
        { required: true, message: '请输入用户名', trigger: 'blur' },
        { min: 3, max: 20, message: '用户名长度在 3 到 20 个字符', trigger: 'blur' }
    ],
    userPass: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        { min: 6, max: 20, message: '密码长度在 6 到 20 个字符', trigger: 'blur' }
    ],
    userNickName: [
        { required: true, message: '请输入昵称', trigger: 'blur' },
        { min: 2, max: 10, message: '昵称长度在 2 到 10 个字符', trigger: 'blur' }
    ],
    roleId: [
        { required: true, message: '请选择角色', trigger: 'change' }
    ]
};

const register = () => {
    formRef.value.validate((valid: boolean) => {
        if (!valid) {
            return;
        }

        Write.post("/Management/LoginCreate", form).then(res => {
            if (res.data.code == 200) {
                ElMessage.success('注册成功！');
                router.push('/');
            } else {
                ElMessage.error('注册失败！');
                return;
            }
        }).catch(err => {
            ElMessage.error('注册失败：' + (err?.response?.data?.message || '网络错误'));
        });
    });
};

const goLogin = () => {
    router.push('/');
};
</script>

<style scoped>
.login-bg {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    z-index: 1;
    overflow: hidden;
}

.login-bg::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><defs><pattern id="grain" width="100" height="100" patternUnits="userSpaceOnUse"><circle cx="50" cy="50" r="1" fill="rgba(255,255,255,0.1)"/></pattern></defs><rect width="100" height="100" fill="url(%23grain)"/></svg>');
    opacity: 0.3;
    pointer-events: none;
}

.login-container {
    position: relative;
    z-index: 10;
    width: 100%;
    max-width: 420px;
    padding: 20px;
}

.login-card {
    background: rgba(255, 255, 255, 0.95);
    backdrop-filter: blur(20px);
    border-radius: 24px;
    padding: 48px 40px;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.15);
    border: 1px solid rgba(255, 255, 255, 0.2);
    position: relative;
    overflow: hidden;
}

.login-card::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: linear-gradient(135deg, rgba(255, 255, 255, 0.1) 0%, rgba(255, 255, 255, 0.05) 100%);
    border-radius: 24px;
    pointer-events: none;
}

.login-header {
    text-align: center;
    margin-bottom: 40px;
}

.logo-section {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 12px;
}

.logo-icon {
    font-size: 3rem;
    animation: bounce 2s infinite;
    filter: drop-shadow(0 4px 8px rgba(0, 0, 0, 0.2));
}

@keyframes bounce {

    0%,
    20%,
    50%,
    80%,
    100% {
        transform: translateY(0);
    }

    40% {
        transform: translateY(-8px);
    }

    60% {
        transform: translateY(-4px);
    }
}

.login-title {
    font-size: 2.2rem;
    font-weight: 700;
    color: #2c3e50;
    margin: 0;
    letter-spacing: 2px;
    text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.login-subtitle {
    font-size: 1rem;
    color: #7f8c8d;
    margin: 0;
    font-weight: 500;
}

.login-form {
    position: relative;
    z-index: 5;
}

.form-group {
    margin-bottom: 24px;
}

.input-wrapper {
    position: relative;
    transition: all 0.3s ease;
}

.input-wrapper:hover {
    transform: translateY(-2px);
}

.custom-input {
    border-radius: 12px;
    transition: all 0.3s ease;
}

.custom-input :deep(.el-input__wrapper) {
    background: rgba(255, 255, 255, 0.8);
    border: 2px solid rgba(64, 158, 255, 0.2);
    border-radius: 12px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
    transition: all 0.3s ease;
}

.custom-input :deep(.el-input__wrapper:hover) {
    border-color: rgba(64, 158, 255, 0.4);
    box-shadow: 0 6px 16px rgba(64, 158, 255, 0.15);
}

.custom-input :deep(.el-input__wrapper.is-focus) {
    border-color: #409EFF;
    box-shadow: 0 8px 24px rgba(64, 158, 255, 0.25);
}

.custom-input :deep(.el-input__inner) {
    font-size: 1rem;
    padding: 16px 20px;
    color: #2c3e50;
}

.custom-input :deep(.el-input__prefix) {
    color: #409EFF;
    font-size: 1.1rem;
}

.custom-select {
    width: 100%;
}

.custom-select :deep(.el-input__wrapper) {
    background: rgba(255, 255, 255, 0.8);
    border: 2px solid rgba(64, 158, 255, 0.2);
    border-radius: 12px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
    transition: all 0.3s ease;
}

.custom-select :deep(.el-input__wrapper:hover) {
    border-color: rgba(64, 158, 255, 0.4);
    box-shadow: 0 6px 16px rgba(64, 158, 255, 0.15);
}

.custom-select :deep(.el-input__wrapper.is-focus) {
    border-color: #409EFF;
    box-shadow: 0 8px 24px rgba(64, 158, 255, 0.25);
}

.custom-select :deep(.el-input__inner) {
    font-size: 1rem;
    padding: 16px 20px;
    color: #2c3e50;
}

.form-actions {
    margin-top: 32px;
}

.register-btn {
    width: 100%;
    height: 52px;
    border-radius: 12px;
    font-size: 1.1rem;
    font-weight: 600;
    background: linear-gradient(135deg, #67c23a 0%, #85ce61 100%);
    border: none;
    box-shadow: 0 8px 24px rgba(103, 194, 58, 0.3);
    transition: all 0.3s ease;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
}

.register-btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 12px 32px rgba(103, 194, 58, 0.4);
    background: linear-gradient(135deg, #85ce61 0%, #67c23a 100%);
}

.login-link {
    text-align: center;
    margin-top: 20px;
}

.login-text {
    font-size: 0.95rem;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.3s ease;
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 8px 16px;
    border-radius: 8px;
}

.login-text:hover {
    background: rgba(64, 158, 255, 0.1);
    transform: translateY(-1px);
}

/* 响应式设计 */
@media (max-width: 480px) {
    .login-container {
        padding: 16px;
    }

    .login-card {
        padding: 32px 24px;
    }

    .login-title {
        font-size: 1.8rem;
    }
}
</style>
